package mongo.db.clients.firsts;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class UpdateUserName {

	static Gson gson = null;
	List<ServerAddress> seeds = null;
	MongoClient client = null;

	DB db = null;
	static DBCollection collection = null;

	public UpdateUserName() {
		gson = new GsonBuilder().setPrettyPrinting().create();
		// String json = gson.toJson(obj);

		seeds = new ArrayList<ServerAddress>();

		try {
			seeds.add(new ServerAddress("202.191.136.234", 27017));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		client = new MongoClient(seeds);

		db = client.getDB("dopinventory");
		collection = db.getCollection("devices");
	}

	public static void main(String[] args) {
		
		UpdateUserName up = new UpdateUserName();

/*		DBObject find = new BasicDBObject("_id", "10.1.0.10");
		
		up.findAndPrint(find);

		System.out.println("-------------------------------------------------------------------");

		DBObject update = new BasicDBObject().append("poll.sStatus", 8).append("poll.ssFrom", new Date());
		update.put("poll.sStatus", 3);
		update.put("poll.ssFrom", new Date());

		DBObject finalUpdate = new BasicDBObject("$set", update);

		collection.update(find, finalUpdate);

		up.findAndPrint(find);*/
		
		up.printDBStats();

	}

	private static void findAndPrint(DBObject find) {
		DBCursor iter = collection.find(find);

		while (iter.hasNext()) {
			System.out.println(gson.toJson(iter.next()));
		}
	}
	
	public static void printDBStats() {
		
		DBObject findAllThree = new BasicDBObject("poll.pStatus", new BasicDBObject("$exists" , 1)).append("poll.sStatus", new BasicDBObject("$exists" , 1)).append("poll.dStatus", new BasicDBObject("$exists" , 1));
		DBObject findSAlone = new BasicDBObject("poll.pStatus", new BasicDBObject("$exists" , 0)).append("poll.sStatus", new BasicDBObject("$exists" , 1)).append("poll.dStatus", new BasicDBObject("$exists" , 0));
		DBObject findPS = new BasicDBObject("poll.pStatus", new BasicDBObject("$exists" , 1)).append("poll.sStatus", new BasicDBObject("$exists" , 1)).append("poll.dStatus", new BasicDBObject("$exists" , 0));
		
		DBObject findPoll = new BasicDBObject("poll", new BasicDBObject("$exists" , 1));
		DBObject findConfig = new BasicDBObject("config", new BasicDBObject("$exists" , 1));
		DBObject findDiscover = new BasicDBObject("discover", new BasicDBObject("$exists" , 1));
		
		System.out.println(collection.count(findSAlone));
		System.out.println(collection.count(findAllThree));
		System.out.println(collection.count(findPS));
		
		
	}
}
